package com.ybkj.daijia.server.errand;

import com.ybkj.daijia.server.model.Area;
import com.ybkj.daijia.server.model.AreaWeight;
import com.ybkj.daijia.server.model.ChargeStartDistance;
import com.ybkj.daijia.server.model.ChargeStartNewWeight;
import com.ybkj.daijia.server.model.ChargeStartTime;
import com.ybkj.daijia.server.model.ChargeStartWeight;
import com.ybkj.daijia.server.model.ChargeWeightDistance;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.springframework.util.CollectionUtils;

public class SelectPrice {

    /**
     * 起步费用
     */
    public BigDecimal qbCost;
    /**
     * 等候费用
     */
    public BigDecimal waitCost;
    /**
     * 行驶时间费用
     */
    public BigDecimal travalTimeCost;
    /**
     * 里程费
     */
    public BigDecimal disCost;
    public long travelMinutes;
    public BigDecimal shouldCash;
    // 跑腿二期添加重量费
    public BigDecimal weightPrice;
    /**
     * 所有的收费标准时段
     */
    protected List<ChargeStartTime> prices;
    /**
     * 符合条件的收费时段(最终使用的)
     */
    protected ChargeStartTime price;
    private List<ChargeStartWeight> wPrices;

    private ChargeStartWeight wPrice;

    public ChargeStartWeight getwPrice() {
        return wPrice;
    }

    public BigDecimal getWaitCost() {
        return waitCost;
    }

    public BigDecimal getTravalTimeCost() {
        return travalTimeCost;
    }

    public BigDecimal getDisCost() {
        return disCost;
    }

    public ChargeStartTime getPrice() {
        return price;
    }

    /**
     * time分为单位，mileage公里为单位
     **/

    public void setPrice(long time, double mileage, Area area, Date dateTime, Double weight) {

        prices = area.getChargeStartTimes();

        price = selectPrice(dateTime);

        qbCost = qbFee();

        if (null == weight) {
            weight = 0D;
        }

        weightPrice = calcuteWeight(weight);

        shouldCash = qbCost.add(calcuteDrive(mileage))
            .add(calcuteJsTimeCost(time).add(calcuteWeight(weight)));
    }

    protected ChargeStartTime selectPrice(Date dateTime) {

        // 起步时间
        Calendar calendar = Calendar.getInstance(Locale.CHINESE);
        if (null == dateTime) {
            Date outsetTime = new Date(System.currentTimeMillis());
            calendar.setTime(outsetTime);
        } else {
            calendar.setTime(dateTime);
        }
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 小时
        int minute = calendar.get(Calendar.MINUTE); // 分钟
        int hourminute = (hour * 60) + minute;
        for (ChargeStartTime chargeTime : prices) {
            if (!chargeTime.isEmpty()) {
                if (chargeTime.getStartHour() <= chargeTime.getEndHour()) {
                    if (hourminute >= (chargeTime.getStartHour() * 60) + chargeTime.getStartMinute()
                        && hourminute <= (chargeTime.getEndHour() * 60) + chargeTime
                        .getEndMinute()) {
                        price = chargeTime;
                        return chargeTime;
                    }
                } else {
                    int start1 = (chargeTime.getStartHour() * 60) + chargeTime.getStartMinute();
                    int end1 = 24 * 60;

                    int start2 = 0;
                    int end2 = (chargeTime.getEndHour() * 60) + chargeTime.getEndMinute();

                    if ((hourminute >= start1 && hourminute <= end1) || (hourminute >= start2
                        && hourminute <= end2)) {
                        price = chargeTime;
                        return chargeTime;
                    }
                }

            }
        }
        return null;

        // // 起步时间
        // Date outsetTime = new Date(System.currentTimeMillis());
        // Calendar calendar = Calendar.getInstance(Locale.CHINESE);
        // calendar.setTime(outsetTime);
        // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 小时
        // int minute = calendar.get(Calendar.MINUTE); // 分钟
        // for (ChargeStartTime chargeTime : prices) {
        // int startHour = chargeTime.getStartHour(); // 开始小时
        // int startMinute = chargeTime.getStartMinute(); // 开始分钟
        // int endHour = chargeTime.getEndHour(); // 结束小时
        // int endMinute = chargeTime.getEndMinute(); // 结束分钟
        // if (startHour > endHour) { // 过夜设置
        // if (hour > startHour) {
        // price=chargeTime;
        // return chargeTime;
        // }else if (hour == startHour) {
        // if (minute >= startMinute) {
        // price=chargeTime;
        // return chargeTime;
        // }
        // }else if (hour < startHour) {
        // if (hour < endHour) {
        // price=chargeTime;
        // return chargeTime;
        // }else if (hour == endHour) {
        // if (minute <= endMinute) {
        // price=chargeTime;
        // return chargeTime;
        // }
        // }
        // }
        // }else if (startHour == endHour) {
        // if (startMinute == endMinute) {// 全天
        // price=chargeTime;
        // return chargeTime;
        // }else if (startMinute < endMinute) { // 几十分钟
        // if (hour == startHour && minute <= endMinute) { // 时间在这几十分钟范围内
        // price=chargeTime;
        // return chargeTime;
        // }
        // }else if (startMinute > endMinute) {
        // // 貌似 没有 08:59 -08:00
        // }
        // }else{// 未过夜设置
        // if (hour > startHour) {
        // if (hour < endHour) {
        // price=chargeTime;
        // return chargeTime;
        // } else if (hour == endHour) {
        // if (minute <= endMinute) {
        // price=chargeTime;
        // return chargeTime;
        // }
        // }
        // } else if (hour == startHour) {
        // if (minute >= startMinute) {
        // if (hour < endHour) {
        // price=chargeTime;
        // return chargeTime;
        // } else if (hour == endHour) {
        // if (minute <= endMinute) {
        // price=chargeTime;
        // return chargeTime;
        // }
        // }
        // }
        // }
        // }
        // }
        // return null;
    }

    protected BigDecimal qbFee() {
        if (null == prices || prices.size() == 0) {
            return BigDecimal.ZERO;
        }
        return price.getCost();
    }

    /**
     * 计算里程费用 distance 千米为单位(总的里程数)
     *
     * @return
     */
    public BigDecimal calcuteDrive(double distance) {
        if (null == price) {
            return BigDecimal.ZERO;
        }

        double qblc = price.getQblc(); // 公里
        if (distance <= qblc) {
            return BigDecimal.ZERO;
        }

        double difflc = distance - qblc; // 多出来的里程数
        double djglUnit = price.getDjglUnit();
        long countTimes = (long) (difflc / djglUnit); // 计费次数
        double mod = difflc % djglUnit;
        if (mod != 0) {
            double djglThresholdt = price.getDjglThresholdt();
            if (mod >= djglThresholdt) {
                countTimes++;
            }
        }
        BigDecimal djglCost = price.getDjglCost();// 每次循环的金额
        BigDecimal fee = BigDecimal.ZERO;
        List<ChargeStartDistance> distances = price.getChargeStartDistances();
        if (!CollectionUtils.isEmpty(distances)) {
            for (ChargeStartDistance distancePriceInfo : distances) {
                if (distancePriceInfo.getStartdistance() > 0) {// 第二段收费开始
                    if (distance > distancePriceInfo.getStartdistance()
                        && distance <= distancePriceInfo.getDistance()) {
                        disCost = distancePriceInfo.getMoney();
                        return distancePriceInfo.getMoney();
                    }
                } else if (distance >= distancePriceInfo.getStartdistance()
                    && distance <= distancePriceInfo.getDistance()) {// 只针对第一段收费
                    disCost = distancePriceInfo.getMoney();
                    return distancePriceInfo.getMoney();
                }
            }
        }

        if (distances != null && distances.size() > 0) {
            fee = distances.get(distances.size() - 1).getMoney();
        }
        disCost = new BigDecimal(countTimes).multiply(djglCost).add(fee);
        return disCost;
    }

    /*
     * 计算驾驶时间超出的费用
     *
     * @return
     */
    public BigDecimal calcuteJsTimeCost(long travelTime) {
        travelMinutes = travelTime;
        if (null == price) {
            return BigDecimal.ZERO;
        }
        double qbsj = price.getQbsj();
        double djsjUnit = price.getDjsjUnit();
        double djsjThresholdt = price.getDjsjThresholdt();
        BigDecimal djsjCost = price.getDjsjCost();

        if (travelTime <= qbsj) {
            travalTimeCost = BigDecimal.ZERO;
        } else {
            if (djsjUnit > 0) {
                long extraTime = travelTime - (long) qbsj;
                int unitnum = (int) (extraTime / djsjUnit);
                int unityu = (int) (extraTime % djsjUnit);
                if (unityu > djsjThresholdt) {
                    unitnum++;
                }
                travalTimeCost = new BigDecimal(unitnum).multiply(djsjCost);
            } else {
                travalTimeCost = BigDecimal.ZERO;
            }
        }
        return travalTimeCost;
    }

    /**
     * 重量计费
     *
     * @param weight
     * @return
     */
    public BigDecimal calcuteWeight(double weight) {
        if (null == price) {
            return BigDecimal.ZERO;
        }
        List<ChargeStartNewWeight> weights = price.getChargeStartNewWeights();
        if (!CollectionUtils.isEmpty(weights)) {
            for (ChargeStartNewWeight weightPriceInfo : weights) {
                if (weightPriceInfo.getStartweight() > 0) {// 第二段收费开始
                    if (weight > weightPriceInfo.getStartweight() && weight <= weightPriceInfo
                        .getWeight()) {
                        weightPrice = weightPriceInfo.getMoney();
                        return weightPriceInfo.getMoney();
                    }
                } else if (weight >= weightPriceInfo.getStartweight() && weight <= weightPriceInfo
                    .getWeight()) {// 只针对第一段收费
                    weightPrice = weightPriceInfo.getMoney();
                    return weightPriceInfo.getMoney();
                }
            }
        }
        double cgzl = price.getCgzl(); // 重量
        if (weight <= cgzl) {
            return BigDecimal.ZERO;
        }
        double difflc = weight - cgzl; // 多出来的重量数
        double zlUnit = price.getZlUnit();
        long countTimes = (long) (difflc / zlUnit); // 计费次数
        double mod = difflc % zlUnit;
        if (mod != 0) {
            double zlThresholdt = price.getZlThresholdt();
            if (mod >= zlThresholdt) {
                countTimes++;
            }
        }
        BigDecimal zlCost = price.getZlCost();// 每次循环的金额
        BigDecimal fee = BigDecimal.ZERO;
        if (weights != null && weights.size() > 0) {
            fee = weights.get(weights.size() - 1).getMoney();
        }
        weightPrice = new BigDecimal(countTimes).multiply(zlCost).add(fee);
        return weightPrice;
    }

    public void setWeightPrice(double weight, AreaWeight area) {
        // 获取收费信息
        wPrices = area.getChargeStartWeight();
        //
        for (int i = 0; i < wPrices.size(); i++) {
            ChargeStartWeight chargeStartWeight = wPrices.get(i);
            List<ChargeWeightDistance> chargeWeightDistances = chargeStartWeight
                .getChargeWeightDistances();
            if (chargeStartWeight.getStartWeight() >= weight) {
                wPrice = chargeStartWeight;
                // 重量阈值初始值大于或等于商品重量，按阶段收费
                for (int j = 0; j < chargeWeightDistances.size(); j++) {
                    if (chargeWeightDistances.get(j).getStartdistance() < weight
                        && chargeWeightDistances.get(j).getDistance() >= weight) {
                        shouldCash = chargeWeightDistances.get(j).getMoney();
                    }
                }
            } else {
                wPrice = chargeStartWeight;
                // 重量阈值初始值小于商品重量
                // 获取超过重量
                double overWeight = weight - chargeStartWeight.getStartWeight();
                // 根据超过重量计算实际需要算钱的超过重量
                long countWeight = (long) (overWeight / chargeStartWeight.getAddWeight()); // 计费次数
                double mod = overWeight % chargeStartWeight.getAddWeight();
                if (mod != 0) {
                    double djglThresholdt = chargeStartWeight.getOverWeight();
                    if (mod > djglThresholdt) {
                        countWeight++;
                    }
                }
                BigDecimal addMoney = new BigDecimal(countWeight)
                    .multiply(chargeStartWeight.getCost());
                if (null == addMoney) {
                    addMoney = BigDecimal.ZERO;
                }
                if (null != chargeWeightDistances && chargeWeightDistances.size() > 0) {
                    shouldCash = chargeWeightDistances.get(chargeWeightDistances.size() - 1)
                        .getMoney();
                }
                if (null == shouldCash) {
                    shouldCash = BigDecimal.ZERO;
                }
                shouldCash = shouldCash.add(addMoney);
            }
        }
    }

}
